values = "23456789TJQKA"
ROYAL_FLUSH, STRAIGHT_FLUSH, FOUR, FULL_HOUSE, FLUSH, STRAIGHT, THREE, TWO_PAIR, PAIR, HIGH, TXT = range(11)

def parseGame(string):
    cards = string.split()
    return (cards[:5], cards[5:])


def isSameSuite(hand):
    def getSuite(card):
        return card[1]

    suite = getSuite(hand[0])
    return all(suite == getSuite(card) for card in hand[1:])
               

def count(hand):
    def getValue(card):
        return values.index(card[0])
    
    result = [0] * len(values)

    for card in hand:
        result[getValue(card)] += 1
        
    return result


def score(hand):
    cards = count(hand)    
    result = [0] * 11

    if isSameSuite(hand):
        cards_str = "".join(str(card) for card in cards)
        pos = cards_str.find("1" * 5)

        if 8 == pos:
            result[ROYAL_FLUSH] = 1
            result[TXT] = "royal flush"           
        elif 0 <= pos:
            result[STRAIGHT_FLUSH] = pos
            result[TXT] = "straight flush upto ", pos + 5
        else:
            result[FLUSH] = 1
            result[TXT] = "flush"

    elif 4 in cards:
        result[FOUR_OF_A_KIND] = cards.index(4)
        #result[HIGH] = [cards.index(1)]
        result[TXT] = "four ", cards.index(4)+2, " and a ", cards.index(1)+2
        
    elif 3 in cards:
        if 2 in cards:
            result[FULL_HOUSE] = [cards.index(3), cards.index(2)]
            result[TXT] = "full house", cards.index(3)+2, "and", cards.index(2)+2
        else:
            result[THREE] = cards.index(3)
            a = cards.index(1)
            b = cards.index(1, a + 1)
            #result[HIGH] = [b, a]
            result[TXT] = "three ", cards.index(3)+1, " and ", b+2, " and ", a+2
            
    elif 2 in cards:
        if 2 == cards.count(2):
            a = cards.index(2)
            b = cards.index(2, a + 1)
            result[TWO_PAIR] = [b, a]
            #result[HIGH] = [cards.index(1)]
            result[TXT] = "two pair", b+2, "and", a+2
        else:
            result[PAIR] = cards.index(2)
            #result[HIGH] = [i for i in xrange(len(cards) - 1, 0, -1) if cards[i] == 1]
            result[TXT] = "pair", cards.index(2) + 2
    else:
        result[HIGH] = [i for i in xrange(len(cards) - 1, 0, -1) if cards[i] == 1]
        result[TXT] = "high", result[HIGH][0] + 2
        
        
        
    return result
   

def test():     
    examples = [
        "JH 5C TD 4C 6H 4H TS 7H 2C 5C",
        "5H 5C 6S 7S KD 2C 3S 8S 8D TD",
        "5D 8C 9S JS AC 2C 5C 7D 8S QH",
        "2D 9C AS AH AC 3D 6D 7D TD QD",
        "4D 6S 9H QH QC 3D 6D 7H QD QS",
        "2H 2D 4C 4D 4S 3C 3D 3S 9S 9D"
    ]
    
    for line in examples:
        (hand1, hand2) = parseGame(line)
        print hand1, "->", count(hand1), "->", score(hand1)
        print hand2, "->", count(hand2), "->", score(hand2)


def solution():
    filename="poker.txt"   
    file = open(filename, "r")
    wins = 0
    
    for line in file:
        (hand1, hand2) = parseGame(line)
        (score1, score2) = (score(hand1), score(hand2))
        if score1 > score2:
            wins += 1
            print hand1, score1[TXT], ">", hand2, score2[TXT]
        elif score1 < score2:
            print hand1, score1[TXT], "<", hand2, score2[TXT]
        else:  
            print "problem", hand1, hand2
            
    print wins
          


from timeit import Timer
print Timer('solution()', 'from __main__ import solution').timeit(1)  
